package com.yueqian.service.impl;

import com.yueqian.common.utils.Logger;
import com.yueqian.mapper.AccountMapper;
import com.yueqian.pojo.Account;
import com.yueqian.service.AccountService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {

//    private static final Log logger = LogFactory.getLog(AccountServiceImpl.class);
      Logger logger = Logger.getLogger(AccountServiceImpl.class);


    @Autowired
    private AccountMapper accountMapper;

    @Override
    public Account findAccountById(Integer id) {
        Account account = accountMapper.findAccountById(id);
        logger.debug("account: " + account);
        return account;
    }

    @Override
    public List<Account> findAll() {
        List<Account> list = accountMapper.findAll();
        logger.info("list: " + list);
        return list;
    }

    @Transactional(rollbackFor = {Exception.class})
//    @Transactional(rollbackForClassName = {"java.lang.Exception"})
//    @Transactional(noRollbackFor = {NullPointerException.class})
//    @Transactional(noRollbackForClassName = "java.lang.NullPointerException")
    @Override
    public void update_Account(String sourceName, String targetName, Double money) throws Exception {
        /*try {
            accountMapper.updateAccount(sourceName, String.valueOf(-money));
            int i = 10/0;
            accountMapper.updateAccount(targetName, String.valueOf(+money));
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        }*/
        accountMapper.updateAccount(sourceName, String.valueOf(-money));
        //int i = 10/0;
//        FileInputStream fileInputStream = new FileInputStream("aaa");
//        fileInputStream.read();
        accountMapper.updateAccount(targetName, String.valueOf(+money));
    }
}
